package com.asia5b.a5mall.middleware.model.shop

import androidx.databinding.ObservableBoolean
import android.text.TextUtils
import com.asia5b.a5bmall.resource.ResString
import com.asia5b.a5mall.middleware.R
import com.asia5b.a5mall.middleware.model.base.BaseModel
import com.asia5b.a5mall.middleware.model.bean.CartMultiItemEntity
import com.asia5b.a5mall.middleware.model.member.CouponModel
import com.asia5b.a5mall.middleware.model.member.PromotionModel
import com.asia5b.a5mall.middleware.model.order.OrderActionModel
import com.asia5b.a5mall.middleware.utils.ObserableString
import com.asia5b.a5mall.middleware.utils.StringUtils
import com.asia5b.a5mall.middleware.utils.glide.BannerImageInteface
import com.google.gson.annotations.SerializedName
import com.luck.picture.lib.entity.LocalMedia
import java.io.Serializable

/**
 * Created by zhangyuncai on 2020/5/9.
 */
open class AbsGoodsModel constructor() : Serializable, CartMultiItemEntity, BaseModel() {
    companion object {
        const val hot = 1
        const val new = 2
        const val recommend = 3
    }

    constructor(type: Int) : this() {
        this.type = type
    }

    var type = CartMultiItemEntity.CartGoodsItem

    override val itemType: Int
        get() = type

    /*-------辅助用字段--------*/
    var absGoodsIsChecked = false
        get() {
            return absGoodsChecked == 1
        }

    //商品销售数量文案
    var absGoodsSellCountText = ""
        get() {
            return String.format(ResString.get(R.string.销量_数), "${absGoodsSellCount}")
        }

    //商品好评等级文案
    var absGoodsGradeText = ""
        get() {
            return String.format(ResString.get(R.string.好评_数), "${absGoodsGrade}%")
        }

    //商品销售数量以及好评等级文案
    var absGoodsSellCountAndGradeText = ""
        get() {
            return "${absGoodsGradeText}   ${absGoodsSellCountText}"
        }

    //商品所属店铺是否是自营
    var absGoodsIsSelf = false
        get() {
            return absGoodsSelfOperated==1
        }

    //商品所属收藏id
    var absCollectId = 0
        get() {
            return absId
        }

    //是否被选中
    var absGoodsCheckedOb = ObservableBoolean(false)

    //选中删除
    var checkedDeleteObservable = ObservableBoolean()

    //商品评价内容
    var commentContent = ObserableString("")

    //商品评价5星
    var goodsStar = 5

    //商品评价图片
    var selectionMedia: ArrayList<LocalMedia>? = null

    //商品是否收藏
    var absGoodsCollect = false

    //商品所属店铺信息
    var shopModel: AbsShopModel? = null

    //商品所属优惠券信息
    var couponList: ArrayList<CouponModel>? = null

    //商品所属活动信息
    var promotionList: ArrayList<PromotionModel>? = null
    /*-------辅助用字段--------*/

    //不具备特定意思可能是购物车中的id,可能是收藏中的id,可能是足迹中的id
    @SerializedName(value = "absId", alternate = ["id"])
    var absId = 0

    //本商品是否被选中,后台针对购物车选中与否字段
    @SerializedName(value = "absGoodsChecked", alternate = ["checked"])
    var absGoodsChecked = 0

    //商品名称
    @SerializedName(value = "absGoodsName", alternate = ["goods_name", "name"])
    var absGoodsName = ""


    //商品id
    @SerializedName(value = "absGoodsId", alternate = ["goods_id"])
    var absGoodsId = 0

    //商品图片URL
    @SerializedName(value = "absGoodsImageUrl", alternate = ["goods_image", "goods_img", "thumbnail"])
    var absGoodsImageUrl = ""


    //商品通用价格
    @SerializedName(value = "absGoodsPrice", alternate = ["purchase_price", "goods_price", "price"])
    var absGoodsPrice: Double = 0.0

    //商品通用价格列表
    @SerializedName(value = "absGoodsPriceList", alternate = ["purchase_price_list", "goods_price_list", "price_list"])
    var absGoodsPriceList: HashMap<String, Double>? = null

    //商品重量
    @SerializedName(value = "absGoodsWeightString", alternate = ["goods_weight", "weight"])
    private var absGoodsWeightString = ""
    var absGoodsWeight: Double = StringUtils.parseDouble(absGoodsWeightString)

    //商品原价
    @SerializedName(value = "absGoodsOriginalPrice", alternate = ["original_price"])
    var absGoodsOriginalPrice: Double = 0.0

    //商品原价列表
    @SerializedName(value = "absGoodsOriginalPriceList", alternate = ["original_price_list"])
    var absGoodsOriginalPriceList: HashMap<String, Double>? = null

    //商品库存
    @SerializedName(value = "absGoodsInventory", alternate = ["quantity"])
    var absGoodsInventory = 0

    //商品最大库存
    @SerializedName(value = "absGoodsMaxInventory", alternate = ["enable_quantity"])
    var absGoodsMaxInventory = 0

    //商品所属店铺id
    @SerializedName(value = "absGoodsShopId", alternate = ["seller_id", "shop_id"])
    var absGoodsShopId = 0

    //商品所属店铺名称
    @SerializedName(value = "absGoodsShopName", alternate = ["seller_name"])
    var absGoodsShopName = ""


    //商品所属店铺介绍
    @SerializedName(value = "absGoodsShopIntro", alternate = ["intro"])
    var absGoodsShopIntro = ""


    //商品banner图
    @SerializedName(value = "absGoodsGalleyList", alternate = ["a2Gallery_list", "gallery_list"])
    var absGoodsGalleyList: ArrayList<Goods2GalleryBean>? = null

    //商品参数
    @SerializedName(value = "absGoodsParams", alternate = ["param_list"])
    var absGoodsParamList: ArrayList<Goods2ParamsBean>? = null


    //本商品skuid
    @SerializedName(value = "absGoodsSkuId", alternate = ["sku_id"])
    var absGoodsSkuId = 0

    //商品属性
    @SerializedName(value = "absGoodsSpec")
    var absGoodsSpec = ""
        get() {
            var str: String = ""
            absGoodsSpecModelList?.forEach { spec ->
                str += ("${spec.specName}:${spec.specValue} ")
            }
            if (str.length > 1) {
                str.removeRange(str.length - 1, str.length)
            } else {
                str = ResString.get(R.string.默认)
            }
            if (str.endsWith(",")) {
                str = str.removeSuffix(",")
            }
            return str
        }

    //商品属性列表
    @SerializedName(value = "absGoodsSpecList", alternate = ["spec_list"])
    var absGoodsSpecModelList: ArrayList<SpecModel>? = null

    //商品当前被购买数量=套数*每套数量
    @SerializedName(value = "absGoodsCurrentBuyNum", alternate = ["total_number"])
    var absGoodsCurrentBuyNum = ""

    //商品销售套数(如果退货,以这个为标准)
    @SerializedName(value = "absGoodsSetNum", alternate = ["num"])
    var absGoodsSetNum = 0

    //商品单套退款价格
    @SerializedName(value = "absGoodsReturnPrice", alternate = ["refund_price"])
    var absGoodsReturnPrice = 0.0

    //商品销售数量
    @SerializedName(value = "absGoodsSellCount", alternate = ["buy_count"])
    var absGoodsSellCount = 0

    //商品好评等级
    @SerializedName(value = "absGoodsGrade", alternate = ["grade"])
    var absGoodsGrade = ""


    //是否自营
    @SerializedName(value = "absGoodsSelfOperated", alternate = ["self_operated"])
    var absGoodsSelfOperated: Int = 0


    //购买商品所属会员id
    @SerializedName(value = "absGoodsMemberId", alternate = ["member_id"])
    var absGoodsMemberId = 0

    //针对商品的操作类型
    @SerializedName(value = "absGoodsAction", alternate = ["goods_operate_allowable_vo"])
    var absGoodsAction: OrderActionModel? = null

    //商品下架状态  0:下架 1:上架
    @SerializedName(value = "absGoodsOff", alternate = ["market_enable"])
    var absGoodsOff = 1


    //商品下架状态  0:下架 1:上架,todo goods_off这个字段后台禁止使用
//    @SerializedName(value = "absGoodsOff", alternate = ["goods_off"])
//    var absGoodsOff = 1

}


class Goods2ParamsBean : BaseModel() {
    @SerializedName(value = "name", alternate = ["group_name"])
    var name: String? = null

    var params: ArrayList<Goods2ParamsChildBean>? = null
}

class Goods2ParamsChildBean : BaseModel() {
    @SerializedName(value = "name", alternate = ["param_name"])
    var name: String? = null

    @SerializedName(value = "value", alternate = ["param_value"])
    var value: String? = null
}

class Goods2GalleryBean : BaseModel(), BannerImageInteface {

    var big: String? = null
    var small: String? = null
    var original: String? = null

    override fun showImageUrl(): String {
        return big ?: ""
    }
}

class SpecModel : BaseModel() {
    @SerializedName(value = "specValueId", alternate = ["spec_value_id"])
    val specValueId = 0

    /**规格ID值*/
    @SerializedName(value = "specId", alternate = ["spec_id"])
    val specId = 0

    /**规格ID*/
    @SerializedName(value = "specName", alternate = ["spec_name"])
    val specName: String? = null

    /**规格名*/
    @SerializedName(value = "specValue", alternate = ["spec_value"])
    val specValue: String? = null

    /**规格值名称*/
    @SerializedName(value = "specImage", alternate = ["spec_image"])
    val specImage: String? = null

    /**规格默认图*/
    @SerializedName(value = "specType", alternate = ["spec_type"])
    val specType = 0

    /**规格Type*/
    var select: Boolean = false
}